
from asyncio import current_task
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_scoped_session
import aioredis
from aioredis import Redis
import redis as redis_sync

engine = create_async_engine("mysql+aiomysql://root:new_password@127.0.0.1:3306/{{Snake .projectName}}")

sessionLocal = sessionmaker(
    class_=AsyncSession,
    autocommit=False,
    autoflush=False,
    bind=engine
)

async def get_db_session() -> AsyncSession:
    async with sessionLocal() as session:
        yield session

async def get_async_session() -> async_scoped_session:
    return async_scoped_session(sessionLocal, scopefunc=current_task)

redis_pool = aioredis.ConnectionPool.from_url(
    "redis://127.0.0.1", port=6379, encoding="utf-8", decode_responses=True
)

async def get_async_redis() -> Redis:
    return aioredis.Redis(connection_pool=redis_pool)

sync_pool = redis_sync.ConnectionPool(host='127.0.0.1', port=6379, decode_responses=True)

def get_sync_redis():
    return redis_sync.Redis(connection_pool=sync_pool)

